package com.lanswon.qzsmk.controller.cardManageController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.lanswon.qzsmk.base.DwzPage;
import com.lanswon.qzsmk.base.HttpContants;
import com.lanswon.qzsmk.model.CardLoss;
import com.lanswon.qzsmk.service.cardManageService.LossAndUnbindService;
import com.lanswon.qzsmk.service.systemManageService.ParamManageService;
import com.lanswon.qzsmk.vo.LossCardInfo;
import com.lanswon.qzsmk.vo.ResultVo;
import com.lanswon.qzsmk.vo.VoucherVo;

/**
 * 挂失controller
 * @author guyawei
 *
 */
@Controller
@RequestMapping("/loss")
public class LossController {
	
	@Autowired
	LossAndUnbindService lbs;
	
	@Autowired
	ParamManageService pm;
	
	/**
	 * 跳转挂失查询页面
	 * @return
	 */
	@RequiresPermissions("loss:index")
	@RequestMapping(value="/queryLossCard",name="挂失,loss:index",method={RequestMethod.GET,RequestMethod.POST})
	public String queryLossCard(){
		
		return "LossCard";
		
	}
	
	/**
	 * query需要挂失的信息
	 * @param lci
	 * @param pageCount
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@RequiresPermissions("loss:getLoosCardInfo")
	@RequestMapping(value="/getLoosCardInfo",name="挂失查询挂失信息,loss:getLoosCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public DwzPage<LossCardInfo> getLoosCardInfo(LossCardInfo lci,Integer pageNum,Integer numPerPage){
//		System.out.println(lci.getNumPerPage()+";"+lci.getCurrentPage()+","+pageCount);
//		if(lci.getNumPerPage()==null){//页面初次加载
//			lci.setCurrentPage(1);
//			lci.setNumPerPage(20);
//		}else {//页面分页
//			if(lci.getCurrentPage()<=1||pageCount==0){
//				lci.setCurrentPage(1);
//			}else if(lci.getCurrentPage()>=pageCount&&pageCount!=0){
//				lci.setCurrentPage(pageCount);
//			}
//		}
		if(pageNum==null&&numPerPage==null){
			pageNum=1;
			numPerPage=20;	
		}
		lci.setCurrentPage(pageNum);
		lci.setNumPerPage(numPerPage);
		lci.setStartLine((lci.getCurrentPage()-1)*lci.getNumPerPage());
		DwzPage<LossCardInfo> dPage =null;
		ResultVo resultVo=lbs.queryLossCard(lci);
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			dPage=(DwzPage<LossCardInfo>) resultVo.getServiceObject();
		}
		return dPage;	
	}
	
	/**
	 * 处理跳转挂失登记页面的参数
	 * @param lossCard
	 * @param request
	 * @return
	 */
	@RequiresPermissions("loss:goLoosCardrRegister")
	@RequestMapping(value="/goLoosCardrRegister",name="挂失处理跳转挂失登记页面的参数,loss:goLoosCardrRegister",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> goLoosCardrRegister(String [] lossCard,HttpServletRequest request){
		for(String str:lossCard){
			System.out.println(str);
		}
		LossCardInfo lci=new LossCardInfo();
		lci.setCitizenCardNo(lossCard[0]);
		lci.setCardPName(lossCard[1]);
		lci.setPaperTypeName(lossCard[2]);
		lci.setPaperNo(lossCard[3]);
		lci.setCustomerName(lossCard[4]);
		lci.setCardStatus(lossCard[5]);
		request.getSession().setAttribute("lci", lci);
		Map<String, Object> map=new HashMap<>();
		map.put("statusCode", "200");
		map.put("callbackType", "forward");
		map.put("forwardUrl", "loss/lossCardRegister");//post请求
		return map;
	}
	
	/**
	 * 跳转挂失登记页面
	 * @param request
	 * @return
	 */
	@RequiresPermissions("loss:lossCardRegister")
	@RequestMapping(value="/lossCardRegister",name="挂失登记页面,loss:lossCardRegister",method=RequestMethod.POST)
	public String lossCardRegister(HttpServletRequest request){
		
		return "LossRegister";
	}
	
	/**
	 * 获取所有挂失类型和session中的内容
	 * @return
	 */
	@RequiresPermissions("loss:getLossInfo")
	@RequestMapping(value="/getLossInfo",name="挂失获取所有挂失类型和session中的内容,loss:getLossInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> getLossInfo(HttpServletRequest request){
		LossCardInfo lci=(LossCardInfo) request.getSession().getAttribute("lci");
		request.getSession().removeAttribute("lci");
		Map<String, Object> map=new HashMap<>();
		List<CardLoss> list=pm.queryCardLoss();
		map.put("cardLosses", list);
		map.put("lci", lci);
		return map;
	}
	
	/**
	 * 提交挂失请求
	 * @param lci
	 * @return
	 * @throws Exception 
	 */
	@RequiresPermissions("loss:commitLoosCardInfo")
	@RequestMapping(value="/commitLoosCardInfo",name="挂失提交,loss:commitLoosCardInfo",method=RequestMethod.POST)
	@ResponseBody
	public Map<String, Object> commitLoosCardInfo(LossCardInfo lci,HttpServletRequest request) throws Exception{
		VoucherVo vvo=null;
		ResultVo resultVo=lbs.commitLossCardInfo(lci);//要打印凭证，下面地址需要改变
		Map<String, Object> map=new HashMap<>();
		if(resultVo.getResultStatus().getCode()==HttpContants.RETURN_SUCCESS){
			vvo=(VoucherVo) resultVo.getServiceObject();
			if(lci.getCardStatus().equals("3")){
				map.put("flag", "1");	
				map.put("statusCode", "200");
				map.put("callbackType", "forward");
				map.put("forwardUrl", "loss/queryLossCard");//post请求
				}else{
					request.getSession().setAttribute("vo", vvo);
					request.getSession().setAttribute("type", "lossRegister");
					map.put("max", "true");
			        map.put("title", "挂失凭证");
			        map.put("rel", "loss");
			        map.put("statusCode", "200");
			        map.put("url", "printCredentials/toHairpinPdfView");
			        //20181204 hgl 新增以下三行
			        map.put("jumpToTabid", "lossCard");
			        map.put("jumpToUrl", "loss/queryLossCard");
			        map.put("jumpToTile", "挂失");
				}
			
		}else{
			map.put("statusCode", "300");
			map.put("message", resultVo.getResultStatus().getMessage());
		}
		return map;
	}
}
